package com.zetor.ms.application.service.base.impl;

import com.zetor.ms.application.service.base.ITransactionService;
import com.zetor.ms.core.sequence.SnowflakeIdWorker;
import com.zetor.ms.repository.entity.seata.DemoStockEntity;
import com.zetor.ms.repository.mapper.mybatisplus.DemoStockMapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 事务回滚 demo service
 *
 * @author zlc
 * @date 2021/11/05
 */

@Service
public class TransactionServiceImpl implements ITransactionService {

    @Autowired
    private DemoStockMapper demoStockMapper ;
    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * seataCase4
     *
     * @return
     */
    @Override
    public Long seataCase4(Long orderId) {
        // 本地保存操作
        DemoStockEntity stock = new DemoStockEntity();
        Long id = snowflakeIdWorker.nextId();
        stock.setId(id);
        stock.setOrderId(orderId);
        stock.setCount(1);
        demoStockMapper.insert(stock);
//        int i = 1/0;
        return id;
    }


    /**
     * seataCase4
     *
     * @return
     */
    @Override
    @GlobalTransactional(name = "seata-udate-stock",rollbackFor = Exception.class)
    public void seataCase6(Long id) {
        // 本地保存操作
        demoStockMapper.updateStock(id);
        return;
    }

    /**
     * seataCase4
     *
     * @return
     */
    @Override
    @GlobalTransactional(name = "seata-qry-stock",rollbackFor = Exception.class)
    public Integer seataCase8(Long id) {
        // 本地保存操作
//        DemoStockEntity ent = demoStockMapper.selectById(id);
//        return ent.getCount();
        return demoStockMapper.selectStock(id);
    }

}
